package com.johnson.news.activity;

import android.os.Bundle;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;

import com.johnson.news.R;
import com.johnson.news.activity.base.BaseBindingNormalActivity;
import com.johnson.news.databinding.ActivityNewsPostReplyBinding;
import com.johnson.news.model.bean.BaseResponseData;
import com.johnson.news.model.bean.NewsItem;
import com.johnson.news.model.remote.RemoteDataSource;
import com.johnson.news.util.Utils;
import com.youmbe.library.BaseBindingActivity;
import com.youmbe.library.util.MyToast;

import io.reactivex.functions.Consumer;

public class NewsPostReplyActivity extends BaseBindingNormalActivity<ActivityNewsPostReplyBinding> {
    private InputMethodManager inputMethodManager = null;

    NewsItem newsItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Window window = getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        super.onCreate(savedInstanceState);

        newsItem = getIntent().getParcelableExtra("data");

        inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
//        inputMethodManager.showSoftInput(dataBinding.replyInput,InputMethodManager.SHOW_IMPLICIT);

        init();
    }

    public void init(){
        dataBinding.setOnClickListener(onClickListener);
        dataBinding.replyInput.clearFocus();;

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
        if(keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            overridePendingTransition(0, 0);
            return true;
        }
        return super.onKeyDown(keyCode, keyEvent);
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(view.getId() == R.id.news_post_root){
                finish();
                overridePendingTransition(0, 0);
            } else if(view.getId() == R.id.reply_post){
                String text = dataBinding.replyInput.getText().toString();
                if(TextUtils.isEmpty(text)){
                    MyToast.showToast(getString(R.string.news_post_reply_null), activityInstance);
                    return;
                }

                postReply();

            }
        }
    };


    void postReply() {
        String text = dataBinding.replyInput.getText().toString();

        RemoteDataSource.getRemoteDataSource()
                .newsComment(TextUtils.isEmpty(newsItem.getMid()) ? Utils.MID : newsItem.getMid(), newsItem.getItemid(), text, Utils.getPhoneDeviceId(activityInstance))
                .subscribe(new Consumer<BaseResponseData>() {
                    @Override
                    public void accept(BaseResponseData baseResponseData) throws Exception {
                        if(baseResponseData.status == 1) {
                            MyToast.showToast("发布成功", activityInstance);
                        } else {
                            MyToast.showToast(baseResponseData.msg, activityInstance);

                        }
                    }
                });
    }


    @Override
    public int getContentViewId() {
        return R.layout.activity_news_post_reply;
    }
}
